package com.atguli.server;

import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;


/**
 * @author DPF
 */
public class MyWebClient {


    public static void main(String[] args) {
        int biz_id = 172;
        int state = 11;
        String mobile = "Sari001";
        int source = 3;
        String time = "2025-04-23 11:44:24";
        String user = "yqzx01";
        int dial_result_id = 830215;
//        System.out.println(getExample());
        System.out.println(postExample(biz_id,state,mobile,source,time,user,dial_result_id));
    }

    // GET 请求示例
    public static String getExample() {
        WebClient webClient = WebClient.builder().build();
        String url = "http://192.168.187.113/api/v1/oper/businesses?page=1&pageSize=10&orderBy=time_modified+desc&query=&org_code=&parent_robot_id=&show_table_type=1&business_num=&account_id=";
        String response = "";
        try {
            response = webClient
                    .method(HttpMethod.GET)
                    .uri(url)
                    .header("Authorization", "f3a41e12-3a77-4a42-b7e0-284c3817a36d")
                    .accept(MediaType.APPLICATION_JSON)
                    .retrieve()
                    .bodyToMono(String.class)
                    .block();

        } catch (Exception ex) {
            throw new RuntimeException("service api error: " + ex.getMessage());
        } finally {
            System.out.println("Response: " + response);
        }

        return response;
    }

    public static String postExample(Integer biz_id,Integer state,String mobile,Integer source,String time,String user,Integer dial_result_id) {
        WebClient webClient = WebClient.builder().build();

        String url = "http://192.168.187.113/api/v1/robot/crm/post_contact_log";
        String requestBody = String.format("{\"biz_id\":%d,\"cont_child_state\":%d,\"cont_content\":\"跟进备注\\n\",\"cont_attr\":\"{\\\"跟进标签\\\":[\\\"会到网点\\\",\\\"办理中\\\"],\\\"是否愿意调户\\\":\\\"是\\\"}\",\"mobile\":\"%s\",\"cont_log_source\":%d,\"cont_time\":\"%s\",\"cont_user\":\"%s\",\"dial_result_id\":%d}",
                biz_id, state, mobile, source, time, user, dial_result_id);
        String response = "";
        try {
            response = webClient
                    .method(HttpMethod.POST)
                    .uri(url)
                    .header("Authorization", "11316c4d-e147-4912-8ea9-4ac3387ab1d0")
                    .contentType(MediaType.APPLICATION_JSON)
                    .bodyValue(requestBody)
                    .retrieve()
                    .bodyToMono(String.class)
                    .block();
        } catch (Exception ex) {
            throw new RuntimeException("service api error: " + ex.getMessage());
        } finally {
            System.out.println("Response: " + response);
        }

        return response;
    }

}